class Solution {
public:
    int calculate(string s) {
        stack<int> st;
        int tmp = 0;
        char op = '+';
        int n = s.size(), i = 0;
        while (i < n)
        {
            if (s[i] == ' ') i++;
            else if (s[i] >= '0' && s[i] <= '9')
            {
                int tmp = 0;
                while (s[i] <= '9' && s[i] >= '0')
                    tmp = tmp * 10 + (s[i++] - '0');
                if (op == '+') st.push(tmp);
                else if (op == '-') st.push(-tmp);
                else if (op == '*') st.top() *= tmp;
                else st.top() /= tmp;
            }
            else
            {
                op = s[i++];
            }

        }
        int ret = 0;
        while (!st.empty())
        {
            ret += st.top();
            st.pop();
        }
        return ret;

    }
};